package com.econvent.common.conn.provider;

import com.econvent.common.conn.DataSourceBuilder;
import com.econvent.common.conn.DynamicDataSourceDirector;
import com.econvent.common.conn.entity.BaseDataSourceProperty;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xiekun
 */
@Slf4j
@AllArgsConstructor
public abstract class AbstractDataSourceProvider implements DynamicDataSourceProvider {

    private final DynamicDataSourceDirector dynamicDataSourceDirector ;

    protected Map<String, DataSource> createDataSourceMap(Map<String, BaseDataSourceProperty> dataSourcePropertiesMap) {
        Map<String, DataSource> dataSourceMap = new HashMap<>(dataSourcePropertiesMap.size() * 2);

        for (Map.Entry<String, BaseDataSourceProperty> item : dataSourcePropertiesMap.entrySet()) {
            String dsName = item.getKey();
            BaseDataSourceProperty dataSourceProperty = item.getValue();
            String poolName = dataSourceProperty.getDatasourcename();
            if (poolName == null || "".equals(poolName)) {
                poolName = dsName;
            }

            dataSourceProperty.setDatasourcename(poolName);
            //dataSourceMap.put(dsName, dynamicDataSourceDirector.buildDataSource());
        }

        return dataSourceMap;
    }
}